package com.example.qx.lianxra;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.qx.R;
import com.example.qx.adapter.lianxradapter;
import com.example.qx.database.data;
import com.example.qx.lei.leilianxr;
import com.example.qx.lei.zhangmi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import net.sourceforge.pinyin4j.PinyinHelper;


public class lianxr extends Fragment {
    private static Map<String , Character> firstletter=new HashMap<>() ;
    private data data=new data();
    private zhangmi zhangmi= com.example.qx.lei.zhangmi.getInstance();
    public lianxr() throws SQLException, ClassNotFoundException, IllegalAccessException, java.lang.InstantiationException {}

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup viewGroup, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.lianxr,viewGroup,false);
        List<leilianxr> list=new ArrayList<>();
        final List<Integer>[] listgx = new List[]{null};
        final Connection[] connection = new Connection[1];
        CountDownLatch latch=new CountDownLatch(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
                catch (IllegalAccessException | ClassNotFoundException |
                       java.lang.InstantiationException e) {
                    throw new RuntimeException(e);}
                try {
                    connection[0] = DriverManager.getConnection("jdbc:mysql://sh-cdb-9761pjk2.sql.tencentcdb.com:29081/youqi","root","asd123456");
                    listgx[0] = data.getpy(zhangmi.getZhang(),1,connection[0]);
                    for(int a = 0; a< listgx[0].size(); a++)
                    {
                        leilianxr leilianxr= null;
                        leilianxr = data.getgrxx(listgx[0].get(a),connection[0]);
                        list.add(new leilianxr(leilianxr.getTouxiang(),leilianxr.getMingzi(),leilianxr.getId(),getfirstletter(leilianxr.getMingzi()),leilianxr.getBeijing()));
                    }
                } catch (SQLException e) {throw new RuntimeException(e);}
                latch.countDown();
            }
        }).start();
        try{
            latch.await();
            connection[0].close();
        }
        catch (InterruptedException | SQLException e)
        {e.printStackTrace();}



        RecyclerView recyclerView =view.findViewById(R.id.qblianxr);
        lianxradapter lianxradapter=new lianxradapter(getActivity(),list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
         recyclerView.setAdapter(lianxradapter);
         return  view;
    }
    private static char getfirstletter(String a){
        if(firstletter.containsKey(a))
            return firstletter.get(a);
        char b=a.charAt(0);
        String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(b);
        if(pinyin!=null&&pinyin.length>0)
        {  char  first=pinyin[0].charAt(0);
            firstletter.put(a,first);
            return first;
        }
        return '#';
    }
}